<template>

  <div class="app-container">

    <!--表单-->
    <el-form :inline="true" class="demo-form-inline">

      <el-form-item>
        <el-select v-model="searchObj.type" clearable placeholder="请选择">
          <el-option label="学员登录数统计" value="login_num"/>
          <el-option label="学员注册数统计" value="register_num"/>
          <el-option label="课程播放数统计" value="video_view_num"/>
          <el-option label="每日课程数统计" value="course_num"/>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-date-picker
          v-model="searchObj.begin"
          type="date"
          placeholder="选择开始日期"
          value-format="yyyy-MM-dd"/>
      </el-form-item>


      <el-form-item>
        <el-date-picker
          v-model="searchObj.end"
          type="date"
          placeholder="选择截止日期"
          value-format="yyyy-MM-dd"/>
      </el-form-item>

      <el-button
        :disabled="btnDisabled"
        type="primary"
        icon="el-icon-search"
        @click="showChart()"> 查询
      </el-button>
    </el-form>

    <div class="chart-container">
      <div id="chart" class="chart" style="height:500px;width: 100%"/>
    </div>

  </div>
</template>

<script>
  import * as echarts from 'echarts'
  import sta from '@/api/sta'

  export default {
    name: 'show',
    data() {
      return {
        searchObj:{},
        btnDisabled:false,
        xData:[],
        yData:[]
      }
    },
    methods: {
      showChart(){
        if (this.searchObj && this.searchObj.begin && this.searchObj.end) {
          sta.getDataSta(this.searchObj).then(res=>{
            this.xData = res.data.date
            this.yData =  res.data.num
            console.log(res)
            this.showView()
          })
        }else {
          this.$message.warning("请选择图表类型、日期")
        }
      },

      showView(){
        // 基于准备好的dom，初始化echarts实例
        const myChart = echarts.init(document.getElementById('chart'))
        // 绘制图表
        myChart.setOption({
          title: {
            text: '数据统计'
          },
          tooltip: {
            trigger: 'axis'
          },
          xAxis: {
            type:'category',
            data: this.xData   // 表格外描述
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              name: '数量', // 数据名称
              type: 'line', // 图形形状
              data: this.yData  // 表格内数据
            }
          ]
        })
      }
    }
  }
</script>

<style scoped>

</style>
